#pragma once
#include "Model.h"
#include "dynamic_common_global.h"

class DYNAMIC_COMMON_EXPORT Document : public QObject{
    Q_OBJECT 
    
/// @{ Busy system management
private:
    int _isBusy; // default 0
public:
    void pushBusy(){_isBusy++;}
    void popBusy(){if(_isBusy>=1) _isBusy--;}
    /// Is the document busy? (I/O for example)
    bool isBusy(){ return _isBusy>0; }
/// @}

    
public:
    /// The list of all models in the document
    QList<Model*> models; 
    /// The name of the document, used as label
    QString name;
    /// Filepath where the document is saved (or will be...)
    QString path;    
    /// Has document been modified? (i.e. added/removed a model)
    bool isModified;
    /// Viewpoint transformation (identifies trackball)
    QMatrix4x4 transform;

/// @{ Selection management
private:
    /// the model which is currently selected
    Model* _selectedModel;
public:
    /// Retrieves the selection, exception thrown with empty selection
    Model* selectedModel();
    /// Updates the selection, making sure this is a valid model
    void setSelectedModel(Model* model);
    /// @todo Allow for multiple Selection
    // QList<Model*> selectedModels();
/// @}

public:
    /// Basic document constructor
    Document();
    /// Compute bounding box of all the elements in the scene
    QBox3D bbox();
    /// Add a model (if it's only one becomes selection)
    void addModel(Model* m);
    /// Replace Model, also deleting the memory of the old
    void replaceModel(Model* old_model, Model* new_model);
    /// Is there any model?
    bool isEmpty(){ return models.empty(); }
    /// The models in this layer
    const QList<Model*>& getModels(){ return models; }
    
public: 
    /// Converts document to XML and writes to the desired file
    void writeToXML(QString filename);
    void readFromXML(QString filename);
};
